package com.example.myapplication;


import android.content.ComponentCallbacks;
import android.content.ComponentName;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;

import com.facebook.ads.AudienceNetworkActivity;
import com.facebook.ads.InterstitialAdActivity;

import java.util.ArrayList;

public class adContext extends ContextWrapper {
    private final String packageName;
    private final Context context;

    public adContext(Context context, String packageName) {
        super(context);
        this.context = context;
        if(TextUtils.isEmpty(((CharSequence)packageName))) {
            packageName = context.getPackageName();
        }

        this.packageName = packageName;
    }

    private void goActivity(Intent intent) {
        if(this.context != null) {
            this.context.startActivity(intent);
        }
        else {
            super.startActivity(intent);
        }
    }

    private void goActivities(Intent intent) {
        ComponentName component = intent.getComponent();
        if(component != null && (this.packageName.equals(component.getPackageName()))) {
            intent.setComponent(new ComponentName(this.getBaseContext().getPackageName(), component.getClassName()));
        }
    }

    public Context getApplicationContext() {
        return this;
    }

    public ApplicationInfo getApplicationInfo() {
        ApplicationInfo applicationInfo;
        String currentStackTraceString = LogUtils.getCurrentStackTraceString();
        if(currentStackTraceString.contains("com.google.android.gms")) {
            applicationInfo = super.getApplicationInfo();
            applicationInfo.packageName = this.packageName;
        }
        else if(currentStackTraceString.contains("com.mopub.common.util.ManifestUtils")) {
            applicationInfo = super.getApplicationInfo();
            applicationInfo.flags = 1;
        }
        else {
            applicationInfo = super.getApplicationInfo();
        }

        return applicationInfo;
    }

    public PackageManager getPackageManager() { // go 没写错？？？？
//        PackageManagerProxy v0_2;
//        PackageManager packageManager = null;
//        String currentStackTraceString = LogUtils.getCurrentStackTraceString();
//        if(!currentStackTraceString.contains("com.google.android.gms")) {
//            if(((currentStackTraceString.contains("com.mopub.common")) || (currentStackTraceString.contains("com.facebook.ads"))) && !currentStackTraceString.contains("isGooglePlayServicesAvailable")) {
////                LogUtils.d("myl", "replace pkgNameManager");
//                v0_2 = new PackageManagerProxy(this.context.getPackageManager(), this.packageName, this.context);
//                return v0_2;
//            }
//
//            packageManager = super.getPackageManager();
//            return packageManager;
//        }
//        else if(Build.VERSION.SDK_INT == 26) {
//            packageManager = super.getPackageManager();
//            return packageManager;
//        }
//        else {
//            v0_2 = new PackageManagerProxy(this.context.getPackageManager(), this.packageName, this.context);
//        }
//
//        return v0_2;


        String currentStackTraceString = LogUtils.getCurrentStackTraceString();
        if (currentStackTraceString.contains("com.google.android.gms")) {
            if (Build.VERSION.SDK_INT == 26) {
                return super.getPackageManager();
            }
            return new PackageManagerProxy(this.context.getPackageManager(), this.packageName, this.context);
        } else if ((!currentStackTraceString.contains("com.mopub.common") && !currentStackTraceString.contains(BuildConfig.APPLICATION_ID)) || currentStackTraceString.contains("isGooglePlayServicesAvailable")) {
            return super.getPackageManager();
        } else {

            return new PackageManagerProxy(this.context.getPackageManager(), this.packageName, this.context);
        }
    }

    public String getPackageName() {
        String currentStackTraceString = LogUtils.getCurrentStackTraceString();
        if((currentStackTraceString.contains("com.google.android.gms.common.zze.zzan")) || (currentStackTraceString.contains("com.google.android.gms.DynamiteModulesA")) || (currentStackTraceString.contains("com.google.android.gms.common.internal.zzf.zza"))) {
            currentStackTraceString = this.getBaseContext().getPackageName();
        }
        else if(currentStackTraceString.contains("com.google.android.gms")) {
            currentStackTraceString = this.packageName;
        }
        else {
            if(((currentStackTraceString.contains("com.mopub.common")) || (currentStackTraceString.contains("com.facebook.ads"))) && !currentStackTraceString.contains("isGooglePlayServicesAvailable")) {
//                LogUtils.d("myl", "replace pkgName:" + this.adContext);
                return this.packageName;
            }

            currentStackTraceString = super.getPackageName();
        }

        return currentStackTraceString;
    }

    public Object getSystemService(String name) {
        return super.getSystemService(name);
    }

    public void registerComponentCallbacks(ComponentCallbacks callback) {
        this.context.registerComponentCallbacks(callback);
    }

    public void startActivities(Intent[] intents) {
        if(intents != null && intents.length > 0) {
            int length = intents.length;
            int i;
            for(i = 0; i < length; ++i) {
                this.goActivities(intents[i]);
            }
        }

        super.startActivities(intents);
    }

    public void startActivities(Intent[] intents, Bundle bundle) {
        if(intents != null && intents.length > 0) {
            int length = intents.length;
            int i;
            for(i = 0; i < length; ++i) {
                this.goActivities(intents[i]);
            }
        }

        super.startActivities(intents, bundle);
    }

    public void startActivity(Intent srcIntent) {
        int flags;
        Bundle bundle;
        Intent intent;
        this.goActivities(srcIntent);
        ComponentName component = srcIntent.getComponent();
        ArrayList<String> arrayList = new ArrayList<String>();
        arrayList.add("com.facebook.ads.AudienceNetworkActivity");
        arrayList.add("com.facebook.ads.InterstitialAdActivity");
        try {

            for(String tmp : arrayList) {
                Class.forName(tmp);
            }
        }
        catch(Throwable v0_1) {
//            goto label_17;
//            LogUtils.d("myl", "[AdReplaceContext startActivity] 没有fb全屏Activity,不进行拦截");
            try {
                this.goActivity(srcIntent);
            }
            catch(Throwable v0_3) {
//                LogUtils.d("myl", "[AdReplaceContext startActivity] startIntent fail:" + v0_1);
            }
        }

        if(component != null) {
            if(AudienceNetworkActivity.class.getName().equals(component.getClassName())) {
                intent = new Intent(this.context, AudienceNetworkActivity.class);
                bundle = srcIntent.getExtras();
                flags = srcIntent.getFlags();
                intent.putExtras(bundle);
                intent.addFlags(flags);
                this.goActivity(intent);
//                LogUtils.d("myl", "intercept startActivity:" + v1.getClassName());
                return;
            }
            else if(InterstitialAdActivity.class.getName().equals(component.getClassName())) {
                intent = new Intent(this.context, InterstitialAdActivity.class);
                bundle = srcIntent.getExtras();
                flags = srcIntent.getFlags();
                intent.putExtras(bundle);
                intent.addFlags(flags);
                this.goActivity(intent);
//                LogUtils.d("myl", "intercept startActivity:" + v1.getClassName());
                return;
            }
            else {
//                LogUtils.d("myl", "startActivity:" + v1.getClassName());
            }
        }

        this.goActivity(srcIntent);
        return;
    }

    public void startActivity(Intent intent, Bundle bundle) {
        this.goActivities(intent);
        super.startActivity(intent, bundle);
    }

    public void unbindService(ServiceConnection connection) {
        try {
            super.unbindService(connection);
        }
        catch(Throwable v0) {
//            Log.e("wbq", "AdReplaceContext unbindService error", v0);
        }
    }

    public void unregisterComponentCallbacks(ComponentCallbacks callback) {
        this.context.unregisterComponentCallbacks(callback);
    }








}

